#include "../DWMRICore/Fiber.h"
#include "../DWMRICore/Scalar.h"

#include <teem\ell.h>

#include <omp.h>

char *fiber_pathname = NULL;
char *dst_pathname = NULL;

char **confPathname = NULL;
char **vertPathname = NULL;
int w, h, d, scale;
float stepSize, maxLength;

float fiber_length = 0.0f;

void PrintUsage()
{
	printf("Usage: ComputeFSR -fiber <configure_file> -dst <dst_file> -length <fiber_length>\n");
}

int ParseArguments(const int argc, char *argv[])
{
	for (int i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "-fiber") == 0) {
			fiber_pathname = argv[++i];
		} else if (strcmp(argv[i], "-dst") == 0) {
			dst_pathname = argv[++i];
		} else if (strcmp(argv[i], "-length") == 0) {
			fiber_length = (float)atof(argv[++i]);
		} else {
			return -1;
		}
	}

	if (fiber_pathname == NULL || dst_pathname == NULL || fiber_length < 0.0001f)
		return -2;

	return 0;
}

float ApproximateFTLE(CPoint3F *pos, const float delta)
{
	float d = 2.0f * delta;

	float matJ[3][3];
	matJ[0][0] = (pos[0].m_x - pos[1].m_x)  * d;
	matJ[1][0] = (pos[0].m_y - pos[1].m_y)  * d;
	matJ[2][0] = (pos[0].m_z - pos[1].m_z)  * d;
	matJ[0][1] = (pos[3].m_x - pos[6].m_x)  * d;
	matJ[1][1] = (pos[3].m_y - pos[6].m_y)  * d;
	matJ[2][1] = (pos[3].m_z - pos[6].m_z)  * d;
	matJ[0][2] = (pos[8].m_x - pos[17].m_x) * d;
	matJ[1][2] = (pos[8].m_y - pos[17].m_y) * d;
	matJ[2][2] = (pos[8].m_z - pos[17].m_z) * d;

	float matJT[3][3];
	matJT[0][0] = matJ[0][0];
	matJT[1][0] = matJ[0][1];
	matJT[2][0] = matJ[0][2];
	matJT[0][1] = matJ[1][0];
	matJT[1][1] = matJ[1][1];
	matJT[2][1] = matJ[1][2];
	matJT[0][2] = matJ[2][0];
	matJT[1][2] = matJ[2][1];
	matJT[2][2] = matJ[2][2];

	double matA[9];
	matA[0] = matJT[0][0] * matJ[0][0] + matJT[0][1] * matJ[1][0] + matJT[0][2] * matJ[2][0];
	matA[1] = matJT[0][0] * matJ[0][1] + matJT[0][1] * matJ[1][1] + matJT[0][2] * matJ[2][1];
	matA[2] = matJT[0][0] * matJ[0][2] + matJT[0][1] * matJ[1][2] + matJT[0][2] * matJ[2][2];

	matA[3] = matJT[1][0] * matJ[0][0] + matJT[1][1] * matJ[1][0] + matJT[1][2] * matJ[2][0];
	matA[4] = matJT[1][0] * matJ[0][1] + matJT[1][1] * matJ[1][1] + matJT[1][2] * matJ[2][1];
	matA[5] = matJT[1][0] * matJ[0][2] + matJT[1][1] * matJ[1][2] + matJT[1][2] * matJ[2][2];
	
	matA[6] = matJT[2][0] * matJ[0][0] + matJT[2][1] * matJ[1][0] + matJT[2][2] * matJ[2][0];
	matA[7] = matJT[2][0] * matJ[0][1] + matJT[2][1] * matJ[1][1] + matJT[2][2] * matJ[2][1];
	matA[8] = matJT[2][0] * matJ[0][2] + matJT[2][1] * matJ[1][2] + matJT[2][2] * matJ[2][2];

	double eigenValues[3];
	ell_3m_eigenvalues_d(eigenValues, matA, AIR_TRUE);
	double ftle = sqrt(eigenValues[0]);
	return (float)ftle;
}


float ComputeFSR(CFiber **fibers, const uint pos, const float delta)
{
	CPoint3F forwardPos[26];
	CPoint3F backwardPos[26];

	CVector3F dir = fibers[0]->m_seed.dir;

	for (int i = 0; i < 26; ++i) {
		CVector3F temp_dir = fibers[i+1]->m_seed.dir;
		if (InnerProduct(dir, temp_dir) > 0.0f ) {
			int index = (fibers[i+1]->m_fCount-1) > pos ? pos : (fibers[i+1]->m_fCount-1);
			forwardPos[i] = fibers[i+1]->m_pF[index];

			index = (fibers[i+1]->m_bCount-1) > pos ? pos : (fibers[i+1]->m_bCount-1);
			backwardPos[i] = fibers[i+1]->m_pB[index];
		} else {
			int index = (fibers[i+1]->m_fCount-1) > pos ? pos : (fibers[i+1]->m_fCount-1);
			backwardPos[i] = fibers[i+1]->m_pF[index];

			index = (fibers[i+1]->m_bCount-1) > pos ? pos : (fibers[i+1]->m_bCount-1);
			forwardPos[i] = fibers[i+1]->m_pB[index];
		}
	}

	float forwardFTLE = ApproximateFTLE(forwardPos, delta);
	float backwardFTLE = ApproximateFTLE(backwardPos, delta);

	float ftle = forwardFTLE > backwardFTLE ? forwardFTLE : backwardFTLE;
	return ftle;
}

int main(int argc, char *argv[])
{
	if (ParseArguments(argc, argv) != 0) {
		PrintUsage();
		return 0;
	}

	/* read the configure file */
	ReadConfigureFile(fiber_pathname, &confPathname, &vertPathname, w, h, d, scale, stepSize, maxLength);

	/* compute FSR for each voxel */
	float *fsr = new float[w*h*d];
	memset(fsr, 0, sizeof(float)*w*h*d);

	CFiber *fibers[3];
	int offset[9];
    offset[ 0] = 0;
    offset[ 1] = -1;
    offset[ 2] = 1;
    offset[ 3] = -w-1;
    offset[ 4] = -w;
    offset[ 5] = -w+1;
    offset[ 6] = w-1;
    offset[ 7] = w;
    offset[ 8] = w+1;

	float length = fiber_length > maxLength ? maxLength : fiber_length;
	uint id = uint(length / stepSize);
	float delta = 1.0f / (float)scale;

	omp_set_num_threads(64);

	for (int z = 1; z < d - 1; ++z) {
		int cw, ch;
		ReadFibers(confPathname[z-1], vertPathname[z-1], &(fibers[0]), cw, ch);
		ReadFibers(confPathname[z+0], vertPathname[z+0], &(fibers[1]), cw, ch);
		ReadFibers(confPathname[z+1], vertPathname[z+1], &(fibers[2]), cw, ch);

		for (int y = 1; y < h - 1; ++y) {
#pragma omp parallel for            
            for (int x = 1; x < w - 1; ++x) {
				int index = y * w + x;
				CFiber *f[27];

				f[0] = &(fibers[1][index]);			// fiber of current voxel

				for (int i = 1; i < 9; ++i) {
					f[i] = &(fibers[1][index+offset[i]]);
				}
				for (int i = 0; i < 9; ++i) {
					f[i+9] = &(fibers[0][index+offset[i]]);
				}
				for (int i = 0; i < 9; ++i) {
					f[i+18] = &(fibers[2][index+offset[i]]);
				}

				fsr[z*w*h+index] = ComputeFSR(f, id, delta);
			}
		}

		delete[] fibers[0];
		delete[] fibers[1];
		delete[] fibers[2];

		printf("z = %d\n", z);

	}


	/* save the result */
	CScalar *result = new CScalar();
	result->CreateScalar(w, h, d, fsr);
	result->SaveScalarFile(dst_pathname);

	delete result;

	for (int z = 0; z < d; ++z) {
		delete[] confPathname[z];
		delete[] vertPathname[z];
	}
	delete[] confPathname;
	delete[] vertPathname;

	delete[] fsr;

	return 0;
}